import os
import re
import threading

import numpy as np
import time
from commen.log import Logger
import openpyxl
import commen.config
def ImuDatacontinuity():
    #---------读取config文件获取筛选字段内容--------------------------
    mcukey = commen.config.global_config.getRaw('keywords', 'mcurtk')
    cankey = commen.config.global_config.getRaw('keywords', 'canrtk')
    # ---------读取config文件获取筛选字段内容--------------------------
    #-----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            if "can" in file:
                aa = os.path.join(root, file)
            if "mcu" in file:
                bb = os.path.join(root, file)
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path+"can到mcu的rtk数据完整性验证结果.xlsx"
    sheet.cell(1, 2).value = "mcurtk数据系统时间"
    sheet.cell(1, 3).value = "mcurtk数据"
    sheet.cell(1, 4).value = "canrtk数据"
    sheet.cell(1, 5).value = "数据是否一致"
    mcurtklist = []
    canrtklist = []
    mcurtksystemtimelist = []

    row =1
    #-----------------------创建excel--------------------------------------------

    logger  = Logger(logger='TestMylog').getlog()
    try:
        canfm = open(aa, 'r', encoding='gbk')  # 打开目标文件
        mcufm = open(bb, 'r', encoding='UTF-8') # 打开目标文件
    except:
        canfm = open(aa, 'r', encoding='gbk')  # 打开目标文件
        mcufm = open(bb, 'r', encoding='gbk')  # 打开目标文件
    logger.info("打开目标文件")
    mculines = mcufm.readlines()
    canlines = canfm.readlines()
    logger.info("获取内容")
    logger.info("开始逐行读取")
    for rtkline in mculines:
        if mcukey in rtkline:
            mcurtksystemtime = rtkline[:rtkline.index("<thread")]
            mcurtkold = rtkline.split("+10:")
            mcurtk = mcurtkold[1].replace("+20:","")
            mcurtk = mcurtk.replace("+30:", "")
            mcurtk = mcurtk.replace("+40:", "")
            mcurtk = mcurtk.replace(")", "")
            mcurtk = mcurtk.replace("-", "")
            mcurtk = mcurtk.replace(" ", "")
            mcurtklist.append(mcurtk[:-3])
            mcurtksystemtimelist.append(mcurtksystemtime)
    for canrtk in canlines:
        if cankey in canrtk:
            canrtkold = canrtk[canrtk.rfind('d 8'):canrtk.rfind('Length')]
            rtkmessage = canrtkold.replace("d 8","")
            rtkmessage1 = rtkmessage.replace(" ","")
            canrtklist.append(rtkmessage1.rstrip())
    number = 0
    logger.info("开始进行对比")
    for i in range(len(mcurtklist)):
        row =row+1
        found = True
        RTK = True
        if int(i)+2 > len(mcurtklist):  # 执行到最后一组四包数据时跳出循环
            break
        if mcurtklist[i][:16] in canrtklist:
            found = False
            canrtknumber = canrtklist.index(str(mcurtklist[i][:16]))
            if canrtknumber +5 > len(canrtklist)+3:
                break

            try:

                canrtkmessage = canrtklist[canrtknumber] + canrtklist[int(canrtknumber) + 1] + canrtklist[int(canrtknumber) + 2] + canrtklist[int(canrtknumber) + 3]
            except:
                print(canrtknumber)
            if i == 0:
                del canrtklist[:canrtknumber]

            if mcurtklist[i] == canrtkmessage:
                RTK = False
                sheet.cell(row, 2).value = mcurtksystemtimelist[i]
                sheet.cell(row, 3).value = mcurtklist[i]
                sheet.cell(row, 4).value = canrtkmessage
                sheet.cell(row, 5).value = "一致"
                # canrtklist[canrtknumber] = "kkkkkkkkkkkkkkkk"
                # canrtklist[int(canrtknumber) + 1] = "kkkkkkkkkkkkkkkk"
                # canrtklist[int(canrtknumber) + 2] = "kkkkkkkkkkkkkkkk"
                # canrtklist[int(canrtknumber) + 3] = "kkkkkkkkkkkkkkkk"
                del canrtklist[:int(canrtknumber) + 4]



                logger.info("正在对比：" + "MCU上" + mcurtklist[i] + "数据与CAN上" + canrtkmessage + "数据是否一致")

        if RTK == True and found == False :
            sheet.cell(row, 2).value = mcurtksystemtimelist[i]
            sheet.cell(row, 3).value = mcurtklist[i]
            sheet.cell(row, 4).value = canrtkmessage
            sheet.cell(row, 5).value = "不一致"
            logger.info("正在对比：" + "MCU上" + mcurtklist[i] + "数据与CAN上" + canrtkmessage + "数据是否一致")

        if found == True:
            sheet.cell(row, 2).value = mcurtksystemtimelist[i]
            sheet.cell(row, 3).value = mcurtklist[i]
            sheet.cell(row, 5).value = "can上无此数据"












    book.save(saveExcel)

ImuDatacontinuity()